﻿using EFCore.Models;
using Microsoft.EntityFrameworkCore;
using SIService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace SService
{
    public class SyskcallotS : SyskcallotSI
    {
        private readonly WmssystemContext myDb;

        public SyskcallotS(WmssystemContext myDb)
        {
            this.myDb = myDb;
        }

        public Task<int> Add(Sys_kc_allot sys_Kc_Allot)
        {
            myDb.Sys_kc_allot.Add(sys_Kc_Allot);
            return myDb.SaveChangesAsync();
        }

        public async Task<int> DbDel(List<int> Ids)
        {
            // 使用 LINQ 查询语句检索相应的记录
            var detailsToDelete = await myDb.Sys_kc_allot
                .Where(d => Ids.Contains(d.ka_kaid))
                .ToListAsync();

            // 删除查询结果
            myDb.Sys_kc_allot.RemoveRange(detailsToDelete);

            // 保存删除操作到数据库
            return await myDb.SaveChangesAsync();
        }

        public async Task<int> DbShen(Sys_kc_allot sys_Kc_Allot, string to)
        {
            // 1. 根据指定条件查找要审核的记录
            var purchaseOrder = await myDb.Sys_kc_allot.FindAsync(sys_Kc_Allot.ka_kaid);

            if (purchaseOrder == null)
            {
                // 如果没有找到对应的记录，可以进行适当的处理，比如抛出异常或者返回错误码
                throw new Exception("指定的记录不存在");
            }
            if (to == "通过")
            {
                purchaseOrder.ka_status = "已审核";
                //把调拨的数量回写到入库明细表中
                var ppList = await myDb.Sys_kc_allot_detail.Where(c => c.ka_number == purchaseOrder.ka_number).ToListAsync();
                foreach (var pp in ppList)
                {
                    var ccList = await myDb.PO_Inventorydetails.Where(c => c.I_IProductCode == pp.ad_product_number).ToListAsync();
                    foreach (var cc in ccList)
                    {
                        cc.I_IQuantity = (int?)(pp.ad_fold_fold - pp.ad_quantity);
                        // 如果需要更新其他属性，也可以在这里进行修改
                    }
                }
            }
            else
            {
                purchaseOrder.ka_status = "未审核";
            }
            // 2. 更新找到的记录的相应字段
            purchaseOrder.ka_auditor = "admin"; // 以此类推，更新需要修改的字段
            purchaseOrder.ka_auditrtime = DateTime.Now;
            purchaseOrder.ka_Reviewcomments = sys_Kc_Allot.ka_Reviewcomments;


            // 3. 将更新后的记录保存回数据库中
            myDb.Sys_kc_allot.Update(purchaseOrder);
            return await myDb.SaveChangesAsync(); // 返回受影响的行数
        }

        public async Task<int> DbUpd(Sys_kc_allot sys_Kc_Allot)
        {
            // 1. 根据指定条件查找要修改的记录
            var purchaseOrder = await myDb.Sys_kc_allot.FindAsync(sys_Kc_Allot.ka_kaid);

            if (purchaseOrder == null)
            {
                // 如果没有找到对应的记录，可以进行适当的处理，比如抛出异常或者返回错误码
                throw new Exception("指定的记录不存在");
            }

            // 2. 更新找到的记录的相应字段
            purchaseOrder.ka_filing_date = sys_Kc_Allot.ka_filing_date; // 以此类推，更新需要修改的字段
            purchaseOrder.ka_type = sys_Kc_Allot.ka_type;
            purchaseOrder.ka_fold = sys_Kc_Allot.ka_fold;
            purchaseOrder.ka_callout = sys_Kc_Allot.ka_callout;
            purchaseOrder.ka_operator = sys_Kc_Allot.ka_operator;
            purchaseOrder.ka_Ramer = sys_Kc_Allot.ka_Ramer;

            // 3. 将更新后的记录保存回数据库中
            myDb.Sys_kc_allot.Update(purchaseOrder);
            return await myDb.SaveChangesAsync(); // 返回受影响的行数
        }

        public Task<List<Sys_kc_allot>> Fye(int pageSize, int currentPage, string Number = null, string Status = null, DateTime? date = null, DateTime? date1 = null)
        {
            // var query = myDb.Sys_kc_allot.AsQueryable();
            var query = from allot in myDb.Sys_kc_allot
                        join warehouse in myDb.Sys_warehouse on allot.ka_callout equals warehouse.w_wid.ToString()
                        join warehouse1 in myDb.Sys_warehouse on allot.ka_fold equals warehouse1.w_wid.ToString()


                        select new Sys_kc_allot
                        {
                            ka_kaid = allot.ka_kaid,
                            ka_number = allot.ka_number,
                            ka_status = allot.ka_status,
                            ka_filing_date = allot.ka_filing_date,
                            ka_type = allot.ka_type,
                            ka_callout = warehouse.w_wname,
                            ka_fold = warehouse1.w_wname,
                            ka_quantity = allot.ka_quantity,
                            ka_money = allot.ka_money,
                            ka_operator = allot.ka_operator,
                            ka_goods = allot.ka_goods,
                            ka_creator = allot.ka_creator,
                            ka_dpum = allot.ka_dpum,
                            ka_documentpreparationtime = allot.ka_documentpreparationtime,
                            ka_auditor = allot.ka_auditor,
                            ka_auditrtime = allot.ka_auditrtime,
                            ka_Ramer = allot.ka_Ramer

                        };

            // 根据条件进行查询
            if (!string.IsNullOrEmpty(Number))
            {
                query = query.Where(c => c.ka_number.Contains(Number));
            }
            if (!string.IsNullOrEmpty(Status))
            {
                query = query.Where(c => c.ka_status == Status);
            }
            if (date != null)
            {
                query = query.Where(e => e.ka_filing_date >= date);
            }
            if (date1 != null)
            {
                query = query.Where(e => e.ka_filing_date <= date1);
            }

            // 分页查询
            return query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        public Task<List<Sys_kc_allot>> Qu()
        {
            return myDb.Sys_kc_allot.ToListAsync();
        }
    }
}
